package com.wave.wavesystem.ai.service.impl;

import com.mybatisflex.core.query.QueryChain;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.wave.common.util.Func;
import com.wave.wavesystem.ai.mapper.AiConversationMessageMapper;
import com.wave.wavesystem.ai.pojo.entity.AiConversationMessage;
import com.wave.wavesystem.ai.pojo.query.AiConversationMessageQuery;
import com.wave.wavesystem.ai.service.IAiConversationMessageService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AiConversationMessageServiceImpl extends ServiceImpl<AiConversationMessageMapper, AiConversationMessage>
        implements IAiConversationMessageService {

    @Override
    public QueryChain<AiConversationMessage> getQueryChain(AiConversationMessageQuery query) {
        QueryChain<AiConversationMessage> queryChain = this.queryChain();
        if (Func.isNotEmpty(query.getConversationId())) {
            queryChain.eq(AiConversationMessage::getConversationId, query.getConversationId());

        }
        queryChain.orderBy(AiConversationMessage::getMessageOrder, false);
        return queryChain;
    }

    @Override
    public List<AiConversationMessage> listByConversationId(String conversationId) {
        return this.queryChain().eq(AiConversationMessage::getConversationId, conversationId)
                .orderBy(AiConversationMessage::getMessageOrder, true).list();
    }

    @Override
    public boolean removeByConversationId(String conversationId) {
        return this.remove(this.query().eq(AiConversationMessage::getConversationId, conversationId));
    }

}
